/**
 * Created by saber on 15/8/13.
 */

wcApp.controller('depsSelectCtrl', ['$scope', 'ionicMaterialInk', 'ionicMaterialMotion','$timeout','$ionicModal','$http','request','$ionicPopup','user','$state','$rootScope','$location',
    function ($scope, ionicMaterialInk, ionicMaterialMotion,$timeout,$ionicModal,$http,request,$ionicPopup,user,$state,$rootScope,$location) {

        function checkLogin() {
            if (!user.hasLogin()) {
                $state.go('app.main')
            }else{
                $http.get($rootScope.baseUrl+'/enroll/ifICan')
                    .success(function (res, code) {
                        if (res.code==208){
                            alert("活动已过期");
                            $state.go('app.main');
                        }
                    })
            }
            if (request.describe != "") {
                $state.go('app.myRequest');
            }

        }

        checkLogin();
        $rootScope.$on("$locationChangeStart", function (e, n, o, ns, os) {
            if ($location.path()=='/departmentList'){
                checkLogin();
            };
        });

        $scope.departmentList=[];

        $scope.curPage=0;

        $scope.perPage=10;

        $scope.hasMore=true;

        $scope.request=request;

        console.log(request);

        //function alert(str){
         //   console.log(str);
        //}

        $scope.getDepartments= function () {
            if ( $scope.hasMore){
                console.log($rootScope.baseUrl+"/enroll/getDepartments/"+$scope.curPage+"/"+$scope.perPage);
                $http.get($rootScope.baseUrl+"/enroll/getDepartments/"+$scope.curPage+"/"+$scope.perPage)
                    .success(function (resp, code, data) {
                        if (code!=200 || resp.code != 200){
                            console.log(resp,code,data);
                        }else{
                            for (var i in resp.data){
                                $scope.departmentList.push(resp.data[i]);

                            }
                            $scope.curPage++;
                            if (resp.data.length<$scope.perPage){
                                $scope.hasMore=false;
                            }

                            $timeout(function(){
                                // Set Motion
                                ionicMaterialMotion.fadeSlideInRight();

                                // Set Ink
                                ionicMaterialInk.displayEffect();
                                $scope.$broadcast('scroll.infiniteScrollComplete');
                            },50)

                            console.log($scope.departmentList,resp);
                        }
                    });
            }else{

                $scope.$broadcast('scroll.infiniteScrollComplete');
            }

        };

        $scope.getDepartments();


        $scope.showDetail=function(obj){
            $scope.curSelected=obj;
            $scope.openModal();
        }

        $scope.doSelect=function(dep,$event){
            $event.stopPropagation();
            if (request.firstWish._id==dep._id){
                request.firstWish={};
                return;
            }
            if (request.secondWish._id==dep._id){
                request.secondWish={};
                return;
            }
            //request.firstWish=dep;
            $scope.popupData={
                selectAs:"",
                selectedDep:dep
            }
            var myPopup = $ionicPopup.show({
                scope: $scope,
                templateUrl:"templates/popup/select.html",
                title: '请选择志愿类型',
                subTitle: '你总共可以填写两个志愿，我们会优先考虑第一志愿',

                buttons: [
                    { text: '取消' },
                    {
                        text: '<b>确认</b>',
                        type: 'button-positive',
                        onTap: function(e) {
                            console.log($scope.popupData.selectAs,"jjj");
                            if (!$scope.popupData.selectAs) {
                                //don't allow the user to close unless he enters wifi password
                                e.preventDefault();
                            } else {
                                request[$scope.popupData.selectAs]=$scope.popupData.selectedDep;
                                console.log(request);
                                return $scope.popupData.selectAs;
                            }
                        }
                    }
                ]
            });

            console.log(request,myPopup);

        };

        $scope.selectSubmit= function () {
            var confirmPopup = $ionicPopup.confirm({
                title: '准备好迎接改变了吗？',
                scope:$scope,
                templateUrl:"templates/popup/confirm.html",
                cancelText:"再考虑下",
                okText:"准备好了"
            });
            confirmPopup.then(function(res) {
                if(res) {
                   $state.go("app.describe")
                } else {
                    console.log('You are not sure');
                }
            });
        }




        $scope.myalert= function (str,$event) {
            if($event){
                $event.stopPropagation();
            }
            console.log(str);
        }
        $ionicModal.fromTemplateUrl('templates/modal/detail.html', {
            scope: $scope,
            animation: 'slide-in-up'
        }).then(function(modal) {
            $scope.modal = modal;
        });
        $scope.openModal = function() {
            $scope.modal.show();
        };
        $scope.closeModal = function() {
            $scope.modal.hide();
        };
        //Cleanup the modal when we're done with it!
        $scope.$on('$destroy', function() {
            $scope.modal.remove();
        });
        // Execute action on hide modal
        $scope.$on('modal.hidden', function() {
            // Execute action
        });
        // Execute action on remove modal
        $scope.$on('modal.removed', function() {
            // Execute action
        });
    }]);